package com.example.yichuangyun.mvvm.ui.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yichuangyun.R;
import com.example.yichuangyun.databinding.ItemCompanyMoreScreenBinding;
import com.example.yichuangyun.dialog.TimePicker;
import com.example.yichuangyun.model.company.MoreScreenModel;
import com.example.yichuangyun.model.company.ScreenModel;
import com.example.yichuangyun.utils.DateUtil;
import com.example.yichuangyun.utils.DensityUtil;
import com.example.yichuangyun.utils.ToastHelper;

import java.util.List;

/**
 * 更多筛选
 */
public class MoreScreenAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final Context context;
    private final List<MoreScreenModel> data;
    private TimePicker timePicker;
    private boolean isStart;

    public MoreScreenAdapter(Context context, List<MoreScreenModel> data) {
        this.context = context;
        this.data = data;
        initTimePickerView();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemCompanyMoreScreenBinding binding = ItemCompanyMoreScreenBinding.inflate(LayoutInflater.from(context), parent, false);
        return new MyViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        MyViewHolder myViewHolder = (MyViewHolder) holder;
        MoreScreenModel model = data.get(position);
        myViewHolder.binding.setMoreScreenModel(model);
        myViewHolder.binding.tvTitle.setPadding(0, position == 0 ? DensityUtil.dip2px(context, 15) : 0, 0, 0);
        if (model.getType() == 1 || model.getType() == 7) {
            myViewHolder.binding.llDate.setVisibility(View.GONE);
            myViewHolder.binding.llCapital.setVisibility(View.VISIBLE);
        } else if (model.getType() == 2) {
            myViewHolder.binding.llDate.setVisibility(View.VISIBLE);
            myViewHolder.binding.llCapital.setVisibility(View.GONE);
        } else {
            myViewHolder.binding.llDate.setVisibility(View.GONE);
            myViewHolder.binding.llCapital.setVisibility(View.GONE);
        }

        List<ScreenModel> tags = model.getTags();
        myViewHolder.binding.tagLayout.setTags(tags);
        myViewHolder.binding.tagLayout.setOnTagListener(mPos -> {
            // 多选
            if (model.getType() == 1 || model.getType() == 2 || model.getType() == 3 || model.getType() == 4
                    || model.getType() == 7 || model.getType() == 8 || model.getType() == 9
                    || model.getType() == 10 || model.getType() == 33) {
                if (myViewHolder.binding.tagLayout.isSelcet(mPos)) {
                    myViewHolder.binding.tagLayout.unSelect(mPos);
                    tags.get(mPos).setSelect(false);
                } else {
                    myViewHolder.binding.tagLayout.setSelect(mPos);
                    tags.get(mPos).setSelect(true);
                }
            } else { // 单选
                if (myViewHolder.binding.tagLayout.isSelcet(mPos)) {
                    myViewHolder.binding.tagLayout.unSelect(mPos);
                    for (ScreenModel screenModel : tags) {
                        screenModel.setSelect(false);
                    }
                } else {
                    myViewHolder.binding.tagLayout.setSelect(mPos);
                    for (ScreenModel screenModel : tags) {
                        screenModel.setSelect(false);
                    }
                    tags.get(mPos).setSelect(true);
                }
                notifyItemChanged(position);
            }
        });

        for (int i = 0; i < tags.size(); i++) {
            if (tags.get(i).isSelect()) {
                myViewHolder.binding.tagLayout.setSelect(i);
            } else {
                myViewHolder.binding.tagLayout.unSelect(i);
            }
        }

        //1、为了避免TextWatcher在第2步被调用，提前将他移除。
        if (myViewHolder.binding.etMinCapital.getTag() instanceof TextWatcher) {
            myViewHolder.binding.etMinCapital.removeTextChangedListener((TextWatcher) (myViewHolder.binding.etMinCapital.getTag()));
        }
        // 第2步：移除TextWatcher之后，设置EditText的Text。
        myViewHolder.binding.etMinCapital.setText(model.getMinCapital());

        TextWatcher watcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                model.setMinCapital(TextUtils.isEmpty(editable.toString()) ? "" : editable.toString());
            }
        };
        myViewHolder.binding.etMinCapital.addTextChangedListener(watcher);
        myViewHolder.binding.etMinCapital.setTag(watcher);

        //1、为了避免TextWatcher在第2步被调用，提前将他移除。
        if (myViewHolder.binding.etMaxCapital.getTag() instanceof TextWatcher) {
            myViewHolder.binding.etMaxCapital.removeTextChangedListener((TextWatcher) (myViewHolder.binding.etMaxCapital.getTag()));
        }
        // 第2步：移除TextWatcher之后，设置EditText的Text。
        myViewHolder.binding.etMaxCapital.setText(model.getMaxCapital());

        TextWatcher watcher2 = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable editable) {
                model.setMaxCapital(TextUtils.isEmpty(editable.toString()) ? "" : editable.toString());
            }
        };
        myViewHolder.binding.etMaxCapital.addTextChangedListener(watcher2);
        myViewHolder.binding.etMaxCapital.setTag(watcher2);

        myViewHolder.binding.tvStartDate.setOnClickListener(view -> {
            isStart = true;
            timePicker.showPickerView();
        });

        myViewHolder.binding.tvEndDate.setOnClickListener(view -> {
            isStart = false;
            timePicker.showPickerView();
        });

        myViewHolder.binding.ivClear.setOnClickListener(view -> {
            data.get(1).setStartDate("");
            data.get(1).setEndDate("");
            notifyItemChanged(1);
        });
    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    static class MyViewHolder extends RecyclerView.ViewHolder {

        ItemCompanyMoreScreenBinding binding;

        public MyViewHolder(ItemCompanyMoreScreenBinding itemCompanyMoreScreenBinding) {
            super(itemCompanyMoreScreenBinding.getRoot());
            binding = itemCompanyMoreScreenBinding;
        }
    }

    private void initTimePickerView() {
        timePicker = new TimePicker((Activity) context, true) {
            @Override
            public void confirm(String date) {
                if (isStart) {
                    if (!TextUtils.isEmpty(data.get(1).getEndDate())) {
                        if (DateUtil.getStringToDate(date) > DateUtil.getStringToDate(data.get(1).getEndDate())) {
                            ToastHelper.show(context.getString(R.string.time_hint1));
                            return;
                        }
                    }
                    data.get(1).setStartDate(date);
                } else {
                    if (!TextUtils.isEmpty(data.get(1).getStartDate())) {
                        if (DateUtil.getStringToDate(data.get(1).getStartDate()) > DateUtil.getStringToDate(date)) {
                            ToastHelper.show(context.getString(R.string.time_hint2));
                            return;
                        }
                    }
                    data.get(1).setEndDate(date);
                }
                notifyItemChanged(1);
            }
        };
    }
}
